package com.nattakute.controller;

import java.util.ArrayList;
import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.nattakute.model.Complaint;
import com.nattakute.model.Status;
import com.nattakute.model.Users;
import com.nattakute.service.ComplaintService;
import com.nattakute.service.StatusService;
import com.nattakute.service.UserService;

@Controller("complaint")
public class ComplaintController {

	@Autowired(required = true)
	@Qualifier(value = "complaintService")
	private ComplaintService complaintService;

	@Autowired(required = true)
	@Qualifier(value = "statusService")
	private StatusService statusService;

	@Autowired(required = true)
	@Qualifier(value = "userService")
	private UserService userService;

	@ModelAttribute("status")
	public List<Status> getStatus() {
		List<Status> status = new ArrayList<Status>();
		status = statusService.getStatus();
		return status;
	}

	@ModelAttribute("users")
	public List<Users> getUsers() {
		List<Users> users = new ArrayList<Users>();
		users = userService.getAll();
		return users;
	}

	@RequestMapping(value = { "/complaint", "/complaint/home" }, method = RequestMethod.GET)
	public String setupComplaint(Model model) {
		List<Complaint> complaints = new ArrayList<Complaint>();
		complaints = complaintService.getComplaint();
		model.addAttribute("complaints", complaints);
		return "complaint/home";
	}

	@RequestMapping(value = "/complaint/add", method = RequestMethod.GET)
	public String complaintForm(@Valid Complaint complaint,
			BindingResult bindingResult, Model model) {
		model.addAttribute("complaint", new Complaint());
		return "complaint/add";
	}

	@RequestMapping(value = "/complaint/add", method = RequestMethod.POST)
	public String addComplaint(
			@ModelAttribute("complaint") @Valid Complaint complaint,
			BindingResult bindingResult, Model model,
			RedirectAttributes redirectAttributes) {
		if (bindingResult.hasErrors()) {
			model.addAttribute("complaint", complaint);
			redirectAttributes.addFlashAttribute("message", "fail");
			return "complaint/add";
		}
		complaintService.addComplaint(complaint);
		model.addAttribute(complaint);
		redirectAttributes.addFlashAttribute("message", "successful");
		return "redirect:/complaint/home";
	}

	@RequestMapping(value = "/complaint/{complaintId}", method = RequestMethod.GET)
	public String editComplaint(
			@PathVariable("complaintId") Integer complaintId, Model model) {
		Complaint c = new Complaint();
		c = complaintService.getComplaintById(complaintId);
		model.addAttribute("complaint", c);
		return "complaint/show";
	}

	@RequestMapping(value = "/complaint/delete")
	public String deleteComplaint(
			@RequestParam(value = "id") Integer complanitId) {
		Complaint c = complaintService.getComplaintById(complanitId);
		complaintService.deleteComplaint(c);
		return "redirect:/complaint/home";
	}

	public void setComplaintService(ComplaintService complaintService) {
		this.complaintService = complaintService;
	}

	public void setStatusService(StatusService statusService) {
		this.statusService = statusService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public ComplaintService getComplaintService() {
		return complaintService;
	}

	public StatusService getStatusService() {
		return statusService;
	}

	public UserService getUserService() {
		return userService;
	}

}
